﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using YJKRevitView;

namespace YJKRevitWndr
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdDoorWinTagInsert : YJKCommandManual
	{
		private void DoInsert(FamilyInstance familyInstance, FamilySymbol tagSymbol)
		{
			try
			{
				if (familyInstance != null)
				{
					if (familyInstance.Category.Id.IntegerValue == -2000023)
					{
						ImpDoorWinTagInsert.Do(this.m_uiDoc, familyInstance, tagSymbol.Id);
					}
					else if (familyInstance.Category.Id.IntegerValue == -2000014)
					{
						ImpDoorWinTagInsert.Do(this.m_uiDoc, familyInstance, tagSymbol.Id);
					}
				}
			}
			catch
			{
			}
		}

		private void SingleDoorTagInsert(ExternalCommandData cmdData, FamilySymbol tagSymbol)
		{
			Selection selection = cmdData.Application.ActiveUIDocument.Selection;
			try
			{
				for (;;)
				{
					Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new DoorFilter(), "请选择要插入编号的门");
					FamilyInstance familyInstance = RevitVersionFuncs.GetElement(this.m_uiDoc.Document, reference.ElementId) as FamilyInstance;
					this.DoInsert(familyInstance, tagSymbol);
				}
			}
			catch
			{
			}
		}

		private void SingleWinTagInsert(ExternalCommandData cmdData, FamilySymbol tagSymbol)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			try
			{
				for (;;)
				{
                    Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new WinFilter(), "请选择要插入编号的窗");
					FamilyInstance familyInstance = RevitVersionFuncs.GetElement(activeUIDocument.Document, reference.ElementId) as FamilyInstance;
					this.DoInsert(familyInstance, tagSymbol);
				}
			}
			catch
			{
			}
		}

		private void SelectionDoorTagInsert(ExternalCommandData cmdData, bool boFilter, FamilySymbol tagSymbol)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			IList<Reference> list = selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new DoorFilter(), "请选择要插入编号的门");
			if (boFilter)
			{
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new DoorFilter(), "请选择特定类型门");
				FamilyInstance familyInstance = RevitVersionFuncs.GetElement(activeUIDocument.Document, reference.ElementId) as FamilyInstance;
				ICollection<ElementId> collection = new List<ElementId>();
				foreach (Reference reference2 in list)
				{
					collection.Add(reference2.ElementId);
				}
				if (familyInstance == null)
				{
					return;
				}
                IEnumerator<Element> enumerator2 = ElementFiltering.FindSpecialInstance(this.m_doc, collection, (BuiltInCategory)familyInstance.Category.Id.IntegerValue, familyInstance.Symbol.Name).GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						Element element = enumerator2.Current;
						FamilyInstance familyInstance2 = element as FamilyInstance;
						this.DoInsert(familyInstance2, tagSymbol);
					}
					return;
				}
			}
			foreach (Reference reference3 in list)
			{
				FamilyInstance familyInstance3 = RevitVersionFuncs.GetElement(activeUIDocument.Document, reference3.ElementId) as FamilyInstance;
				this.DoInsert(familyInstance3, tagSymbol);
			}
		}

		private void SelectionWinTagInsert(ExternalCommandData cmdData, bool boFilter, FamilySymbol tagSymbol)
		{
			Selection selection = cmdData.Application.ActiveUIDocument.Selection;
			IList<Reference> list = selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, new WinFilter(), "请选择要插入编号的窗");
			if (boFilter)
			{
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new WinFilter(), "请选择特定类型窗");
				FamilyInstance familyInstance = RevitVersionFuncs.GetElement(this.m_doc, reference.ElementId) as FamilyInstance;
				ICollection<ElementId> collection = new List<ElementId>();
				foreach (Reference reference2 in list)
				{
					collection.Add(reference2.ElementId);
				}
				if (familyInstance == null)
				{
					return;
				}
                IEnumerator<Element> enumerator2 = ElementFiltering.FindSpecialInstance(this.m_doc, collection, (BuiltInCategory)familyInstance.Category.Id.IntegerValue, familyInstance.Symbol.Name).GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						Element element = enumerator2.Current;
						FamilyInstance familyInstance2 = element as FamilyInstance;
						this.DoInsert(familyInstance2, tagSymbol);
					}
					return;
				}
			}
			foreach (Reference reference3 in list)
			{
				FamilyInstance familyInstance3 = RevitVersionFuncs.GetElement(this.m_uiDoc.Document, reference3.ElementId) as FamilyInstance;
				this.DoInsert(familyInstance3, tagSymbol);
			}
		}

		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)3;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Document document = cmdData.Application.ActiveUIDocument.Document;
			Result result;
			using (DlgDoorWinTagInsert dlgDoorWinTagInsert = new DlgDoorWinTagInsert(cmdData.Application))
			{
				try
				{
					while (dlgDoorWinTagInsert.ShowDialog() == DialogResult.OK)
					{
						List<FamilySymbol> symbols = dlgDoorWinTagInsert.m_useTag.GetSymbols();
						FamilySymbol tagSymbol = null;
						if (symbols.Count > 0)
						{
							tagSymbol = symbols[0];
						}
						if (dlgDoorWinTagInsert.rbtDoor.Checked)
						{
							if (!dlgDoorWinTagInsert.rbSelection.Checked)
							{
								this.SingleDoorTagInsert(cmdData, tagSymbol);
							}
							else
							{
								this.SelectionDoorTagInsert(cmdData, dlgDoorWinTagInsert.cbByType.Checked, tagSymbol);
							}
						}
						else if (!dlgDoorWinTagInsert.rbSelection.Checked)
						{
							this.SingleWinTagInsert(cmdData, tagSymbol);
						}
						else
						{
							this.SelectionWinTagInsert(cmdData, dlgDoorWinTagInsert.cbByType.Checked, tagSymbol);
						}
					}
				}
				catch (Exception)
				{
				}
				result = base.YJKExcute(cmdData, ref message, elements);
			}
			return result;
		}
	}
}
